﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BabyLearnEnglish_srcum_demo
{
    public partial class frmChoi : Form
    {
        int curQNumber = 0;
        int curA = 0;
        int corect = 0;
        int[] arrA = new int[100];
        Question Answer = new Question();

        public frmChoi()
        {
            InitializeComponent();
        }

        private void frmChoi_Load(object sender, EventArgs e)
        {
            curQNumber = 0;
            curA = 0;
            corect = 0;
            arrA = new int[100];
            getQandA();
            setUI();
        }

        public void getQandA()
        {
            Random random;
            int randomNumber;
            for (int i = 0; i < 4; i++)
            {
                int k = i;
                random = new Random();
                randomNumber = random.Next(0, Form1.listQ.Count);
                int pos = Array.IndexOf(arrA, randomNumber);
                if (pos == -1 && Form1.listQ[randomNumber].Result != Form1.listCurQ[curQNumber].Result)
                    arrA[i] = randomNumber;
                else
                    i = k-1;
            }



            rdDapAn1.Text = Form1.listQ[arrA[0]].Result;
            rdDapAn1.Tag = Form1.listQ[arrA[0]];

            rdDapAn2.Text = Form1.listQ[arrA[1]].Result;
            rdDapAn2.Tag = Form1.listQ[arrA[1]];

            rdDapAn3.Text = Form1.listQ[arrA[2]].Result;
            rdDapAn3.Tag = Form1.listQ[arrA[2]];

            rdDapAn4.Text = Form1.listQ[arrA[3]].Result;
            rdDapAn4.Tag = Form1.listQ[arrA[3]];

            random = new Random();
            randomNumber = random.Next(0, 4);
            arrA[randomNumber] = curQNumber * 100;
            curA = randomNumber;
            switch (curA)
            {
                case 0:
                    rdDapAn1.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapAn1.Tag = Form1.listCurQ[curQNumber];
                    break;
                case 1:
                    rdDapAn2.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapAn2.Tag = Form1.listCurQ[curQNumber];
                    break;
                case 2:
                    rdDapAn3.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapAn3.Tag = Form1.listCurQ[curQNumber];
                    break;
                case 3:
                    rdDapAn4.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapAn4.Tag = Form1.listCurQ[curQNumber];
                    break;
                default:
                    break;
            }

            Image image = Image.FromFile("../../data/img/" + Form1.listCurQ[curQNumber].Url);
            pictureBox.Image = image;
        }

        private void btnTiepTuc_Click(object sender, EventArgs e)
        {
            Progress();

            if (curQNumber == Form1.listCurQ.Count - 2)
                btnTiepTuc.Text = "Hoàn tất";
            else if (curQNumber == Form1.listCurQ.Count - 1)
            {
                frmHoanThanh frm = new frmHoanThanh();
                frm.ShowDialog();
                Form1.isFinished = true;
                this.Close();
            }
            if (curQNumber <= Form1.listCurQ.Count - 2)
            {
                curQNumber++;
                getQandA();
            }

            setUI();
        }

        private void btnDungLai_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void Progress()
        {
            rdDapAn1.ForeColor = Color.White;
            rdDapAn2.ForeColor = Color.White;
            rdDapAn3.ForeColor = Color.White;
            rdDapAn4.ForeColor = Color.White;
            if (Answer.Result == Form1.listCurQ[curQNumber].Result)
            {
                corect++;
                Form1.score += Form1.listCurQ[curQNumber].Score;
            }
            rdDapAn1.Checked = true;
        }

        private void rdDapDan1_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (Question)rdDapAn1.Tag;
        }

        private void rdDapDan2_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (Question)rdDapAn2.Tag;
        }

        private void rdDapDan3_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (Question)rdDapAn3.Tag;
        }

        private void rdDapDan4_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (Question)rdDapAn4.Tag;
        }

        private void btnHint_Click(object sender, EventArgs e)
        {
            switch (curA)
            {
                case 0:
                    rdDapAn1.ForeColor = Color.Yellow;
                    break;
                case 1:
                    rdDapAn2.ForeColor = Color.Yellow;
                    break;
                case 2:
                    rdDapAn3.ForeColor = Color.Yellow;
                    break;
                case 3:
                    rdDapAn4.ForeColor = Color.Yellow;
                    break;
                default:
                    break;
            }
        }

        public void setUI()
        {
            if (Form1.Lang == "Vi")
            {
                this.Text = "Cửa sổ trò chơi";
                lbCauHoi.Text = "Câu hỏi";
                lbDiem.Text = "Điểm";
                btnDungLai.Text = "Dừng lại";
                btnTiepTuc.Text = "Tiếp tục";
                btnTroGiup.Text = "Trợ giúp";

                lbCauHoi.Text = "Câu hỏi: " + (curQNumber + 1).ToString() + "/" + (Form1.listCurQ.Count).ToString();
                lbDiem.Text = "Điểm: " + Form1.score.ToString();
            }
            else
            {
                this.Text = "Playing window";
                lbCauHoi.Text = "Question";
                lbDiem.Text = "Score";
                btnDungLai.Text = "Stop";
                btnTiepTuc.Text = "Next";
                btnTroGiup.Text = "Hint";

                lbCauHoi.Text = "Question: " + (curQNumber + 1).ToString() + "/" + (Form1.listCurQ.Count).ToString();
                lbDiem.Text = "Score: " + Form1.score.ToString();
            }
        }
    }
}
